#!/bin/bash

cd /usr/lib/x86_64-linux-gnu/
ln -sf libnvcuvid.so.1 libnvcuvid.so
ln -sf libnvidia-encode.so.1 libnvidia-encode.so
ln -sf libnvidia-fbc.so.1 libnvidia-fbc.so
ln -sf libnvidia-ml.so.1 libnvidia-ml.so
ln -sf libnvidia-opencl.so.1 libnvidia-opencl.so
ln -sf libnvidia-ptxjitcompiler.so.1 libnvidia-ptxjitcompiler.so
ln -sf libnvoptix.so.1 libnvoptix.so
# export MAKEFLAGS="-j$(($(grep -c ^processor /proc/cpuinfo) - 0))"

echo "install ffmpeg"
apt install -y libavfilter-dev libavdevice-dev libopus-dev libvpx-dev libblas-dev liblapack-dev libavformat-dev
apt install -y libfdk-aac-dev libx264-dev libx265-dev libopengl-dev libgl1-mesa-dev yasm
apt install -y autoconf libass-dev libfreetype6-dev libsdl2-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo zlib1g-dev
apt install -y libaom-dev libmp3lame-dev libjpeg-dev libpng-dev
apt install -y nvtop passwd

#^ 编译安装 ffmpeg 需要用到 yasm 汇编器
# libaom 从ffmpeg 4.1版本开始需要依赖

mkdir -p /WLF_temp
cd /WLF_temp
git clone -b n11.0.10.1 --depth=1 https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
git clone -b n5.1.3 --depth=1 https://git.ffmpeg.org/ffmpeg.git

#* -------------------------- nv-codec-headers --------------------------
cd /WLF_temp
cd nv-codec-headers
make install
#* -------------------------- ffmpeg --------------------------
cd /WLF_temp
cd ffmpeg

./configure \
--prefix="/usr/local/ffmpeg/" \
--extra-cflags="-I/usr/local/cuda/include" \
--extra-ldflags="-L/usr/local/cuda/lib64" \
--extra-libs="-lpthread -lm" \
--bindir="/usr/local/ffmpeg/bin" \
--enable-shared \
--enable-ffplay \
--enable-cuda --enable-cuvid --enable-nvenc \
--enable-gpl \
--enable-libaom \
--enable-libass \
--enable-libfdk-aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-libnpp \
--enable-nonfree

make -j $(nproc)
make install


#* -------------------------- 安装完毕，清理 --------------------------
echo "export PATH=/usr/local/ffmpeg/bin:\$PATH" >> ~/.bashrc
echo "export LIBRARY_PATH=/usr/local/ffmpeg/lib:\$LIBRARY_PATH" >> ~/.bashrc
echo "/usr/local/ffmpeg/lib/" >> /etc/ld.so.conf #! 这个才是关键
ldconfig

echo "安装完毕，清理"
rm /WLF_temp/* -rf

#! 注意，这里依赖于自己的 gitlab(毕业之后)
# git clone http://git.x-contion.top:1213/ffmpeg/nv-codec-headers.git
# git clone http://git.x-contion.top:1213/ffmpeg/ffmpeg.git

